package cn.edu.sbs.talent.shiro.mapper;

import cn.edu.sbs.talent.common.mapper.BaseMapper;
import cn.edu.sbs.talent.shiro.entity.Role;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * 角色 Mapper
 *
 * @author ctidy
 * @since 2021/1/7
 */
public interface RoleMapper extends BaseMapper<Role> {

    /**
     * 按账户ID查询角色列表
     *
     * @param accountId 账户ID
     * @return 角色列表
     */
    @Select("SELECT DISTINCT `r`.* FROM `t_role` AS `r` " +
            "JOIN `t_account_role` AS `ar` ON `r`.`id` = `ar`.`role_id` " +
            "WHERE `ar`.`account_id` = #{accountId} " +
            "  AND `r`.`deleted` = 0 " +
            "  AND `ar`.`deleted` = 0 ")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(
                    property = "permissions", column = "id", javaType = List.class,
                    many = @Many(select = "cn.edu.sbs.talent.shiro.mapper.PermissionMapper.selectBatchByRoleId", fetchType = FetchType.LAZY)
            )
    })
    List<Role> selectBatchByAccountId(Long accountId);

    /**
     * 按用户名查询角色列表
     *
     * @param username 用户名
     * @return 角色列表
     */
    @Select("SELECT DISTINCT `r`.* FROM `t_role` AS `r` " +
            "JOIN `t_account_role` AS `ar` ON `r`.`id` = `ar`.`role_id` " +
            "JOIN `t_account` AS `a` ON `a`.`id` = `ar`.`account_id` " +
            "WHERE `ar`.`account_id` = #{accountId} " +
            "  AND `r`.`deleted` = 0 " +
            "  AND `ar`.`deleted` = 0 " +
            "  AND `a`.`deleted` = 0 ")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(
                    property = "permissions", column = "id", javaType = List.class,
                    many = @Many(select = "cn.edu.sbs.talent.shiro.mapper.PermissionMapper.selectBatchByRoleId", fetchType = FetchType.LAZY)
            )
    })
    List<Role> selectBatchByAccountUsername(String username);

    /**
     * 按权限ID查询角色
     *
     * @param permissionId 权限ID
     * @return 角色列表
     */
    @Select("SELECT DISTINCT `r`.* FROM `t_role` AS `r` " +
            "JOIN `t_role_permission` AS `pr` ON `pr`.`role_id` = `r`.`id` " +
            "WHERE `pr`.`permission_id` = #{permissionId} " +
            "  AND `r`.`deleted` = 0 " +
            "  AND `pr`.`deleted` = 0 ")
    List<Role> selectBatchByPermissionId(Long permissionId);

    /**
     * 按权限名查询角色
     *
     * @param name 权限名
     * @return 角色列表
     */
    @Select("SELECT DISTINCT `r`.* FROM `t_role` AS `r` " +
            "JOIN `t_role_permission` AS `pr` ON `pr`.`role_id` = `r`.`id` " +
            "JOIN `t_permission` AS `p` ON `p`.`id` = `pr`.`permission_id` " +
            "WHERE `p`.`name` = #{name} " +
            "  AND `r`.`deleted` = 0 " +
            "  AND `pr`.`deleted` = 0 " +
            "  AND `p`.`deleted` = 0")
    List<Role> selectBatchByPermissionName(String name);

    /**
     * 按 URL 查询角色
     *
     * @param url 权限名
     * @return 角色列表
     */
    @Select("SELECT DISTINCT `r`.* FROM `t_role` AS `r` " +
            "JOIN `t_role_permission` AS `pr` ON `pr`.`role_id` = `r`.`id` " +
            "JOIN `t_permission` AS `p` ON `p`.`id` = `pr`.`permission_id` " +
            "WHERE `p`.`url` = #{url} " +
            "  AND `r`.`deleted` = 0 " +
            "  AND `pr`.`deleted` = 0 " +
            "  AND `p`.`deleted` = 0")
    List<Role> selectBatchByPermissionUrl(String url);

    // TODO t_role 与 t_account_role 级联逻辑删除

    // TODO t_role 与 t_role_permission 级联逻辑删除

}
